package com.iwayin.core.dao.ask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.iwayin.core.dao.BaseDAOImpl;
import com.iwayin.core.dataobject.AskDO;
import com.iwayin.core.vo.SearchVO;

public class AskDAOImpl extends BaseDAOImpl implements AskDAO {

	@Override
	public Long addAsk(AskDO ask) {
		return (Long) executeInsert("AskDAO.addAsk", ask);
	}

	@Override
	public Integer deleteAskById(AskDO ask) {
		return executeUpdate("AskDAO.deleteAskById", ask);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> findHotAsk(AskDO ask) {
		return this.getSqlMapClientTemplate().queryForList("AskDAO.findHotAsk", ask);
	}

	@Override
	public List<Long> findHotAskUserId(AskDO ask) {
		List<AskDO> hotAskList = this.findHotAsk(ask);
		List<Long> hotAskUserIdList = new ArrayList<Long>(hotAskList.size());
		for (AskDO a : hotAskList) {
			hotAskUserIdList.add(a.getUserId());
		}
		return hotAskUserIdList;
	}

	@Override
	public AskDO getAskById(Long userId) {
		return (AskDO) this.getSqlMapClientTemplate().queryForObject("AskDAO.getAskById", userId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> findLastestAsk(AskDO ask) {
		return this.executeQueryForList("AskDAO.findLastestAsk", "AskDAO.findLastestAsk.count", ask);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> findUserAsks(AskDO ask) {
		return this.executeQueryForList("AskDAO.findUserAsks", "AskDAO.findUserAsks.count", ask);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> findFollowAak(AskDO ask) {
		if (ask == null || ask.getFollowIdList() == null || ask.getFollowIdList().size() == 0) {
			return new ArrayList<AskDO>();
		}
		return super.executeQueryForList("AskDAO.findFollowAak", "AskDAO.findFollowAak.count", ask);
	}

	@Override
	public AskDO findAskByAskId(Long askId) {
		return (AskDO) this.getSqlMapClientTemplate().queryForObject("AskDAO.getAskByAskId", askId);
	}

	@Override
	public List<AskDO> findAskMostToday(Long size) {
		return queryForList("AskDAO.findAskMostToday", size);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SearchVO> searchAsks(SearchVO query) {
		return this.executeQueryForList("AskDAO.searchAsks", "AskDAO.searchAsks.count", query);
	}

	@Override
	public Integer askCommentCountPlusOne(Long askId) {
		return executeUpdate("AskDAO.askCommentCountPlusOne", askId);
	}

	@Override
	public Integer askAnswerCountPlusOne(Long askId) {
		return executeUpdate("AskDAO.askAnswerCountPlusOne", askId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> findHotAskList(AskDO query) {
		return this.executeQueryForList("AskDAO.findHotAskList", "AskDAO.findHotAskList.count", query);
	}

	@Override
	public Integer voteCountPlusOne(Long askId) {
		return executeUpdate("AskDAO.voteCountPlusOne", askId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> prevNextAskDetails(Long askId) {
		return this.getSqlMapClientTemplate().queryForList("AskDAO.prevNextAskDetails", askId);
	}

	@Override
	public void likeCountMinusOne(Long askId) {
		this.getSqlMapClientTemplate().update("AskDAO.likeCountMinusOne",askId);
	}

	@Override
	public void likeCountPlusOne(Long askId) {
		this.getSqlMapClientTemplate().update("AskDAO.likeCountPlusOne",askId);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<AskDO> findAskListByIds(List<Long> askIds) {
		if(askIds==null||askIds.size()==0){
			return new ArrayList<AskDO>(1);
		}
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("askIds", askIds);
		return this.getSqlMapClientTemplate().queryForList("AskDAO.findAskListByIds", params);
	}

	@Override
	public List<AskDO> findTop5Ask(String orderBy) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("status", 1);
		params.put("orderBy", orderBy);
		return queryForList("AskDAO.findTop5VoteAsk", params);
	}
}
